package com.astn.adapter.booking;

import java.util.ArrayList;

import com.astn.bks.COfferOnMyRequest;
import com.astn.bks.R;
import com.astn.bks.util.BksConstants;
import com.astn.bks.util.DateUtils;
import com.astn.entity.booking.CRequestHolder;
import com.astn.entity.booking.ProviderHolder;
import com.astn.entity.booking.QuoteRequest;
import com.astn.entity.booking.QuoteTown;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class CRequestAdapter extends BaseAdapter {
	private Context context;
	ArrayList<QuoteRequest> lstItems = new ArrayList<QuoteRequest>();
	LayoutInflater mInflater;

	public CRequestAdapter(Context context, ArrayList<QuoteRequest> lstItems) {
		this.context = context;
		this.lstItems = lstItems;
		mInflater = LayoutInflater.from(this.context);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return lstItems.size();
	}

	@Override
	public QuoteRequest getItem(int position) {
		// TODO Auto-generated method stub
		return lstItems.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return lstItems.get(position).getId();
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		CRequestHolder holder;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.cmyrequestitem, null);
			holder = new CRequestHolder();
			holder.txvDestination = (TextView) convertView
					.findViewById(R.id.txvDestination);
			holder.txvInfo = (TextView) convertView.findViewById(R.id.txvInfo);
			holder.btnDetail = (Button) convertView
					.findViewById(R.id.btnDetail);
			holder.btnOffer = (Button) convertView.findViewById(R.id.btnOffer);
			convertView.setTag(holder);
		} else {
			holder = (CRequestHolder) convertView.getTag();
		}
		String path = "";
		if (lstItems.get(position).getCityname() != null) {
			if (lstItems.get(position).getCityname().trim().length() > 0) {
				path += lstItems.get(position).getCityname() + "-";
			}
		}
		if (lstItems.get(position).getStatename() != null) {
			if (lstItems.get(position).getStatename().trim().length() > 0) {
				path += lstItems.get(position).getStatename() + "-";
			}
		}
		path += lstItems.get(position).getCountryname();
		holder.txvDestination.setText(path);
		String info = "From: "
				+ DateUtils.quoteDateFormat.format(lstItems.get(position)
						.getFrom())
				+ "-To: "
				+ DateUtils.quoteDateFormat.format(lstItems.get(position)
						.getTo()) + " (Adult: "
				+ String.valueOf(lstItems.get(position).getAdult())
				+ ", children: "
				+ String.valueOf(lstItems.get(position).getChildren()) + ")";
		holder.txvInfo.setText(info);
		holder.btnDetail.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Toast.makeText(
						context,
						String.valueOf("Detail: "
								+ lstItems.get(position).getId()),
						Toast.LENGTH_SHORT).show();
			}

		});
		holder.btnOffer.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent i = new Intent(context, COfferOnMyRequest.class);
				i.putExtra(BksConstants.EXTRA_REQUEST, lstItems.get(position));
				((Activity) context).startActivityForResult(i,
						BksConstants.CUSTOMER_OFFER);
			}

		});
		// holder.txvInfo.setText(lstItems.get(position).getCountryid());
		return convertView;
	}

}
